package com.hoshiicloud.as2c.minipro.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.as2c.minipro.dto.ConsumerDto;
import com.hoshiicloud.as2c.minipro.dto.MemberDto;
import com.hoshiicloud.as2c.minipro.dto.OrderDTO;
import com.hoshiicloud.as2c.minipro.dto.PayResultDTO;
import com.hoshiicloud.as2c.minipro.feign.OrderFeignService;
import com.hoshiicloud.as2c.minipro.feign.UserService;
import com.hoshiicloud.as2c.minipro.param.*;
import com.hoshiicloud.common.controller.BaseController;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.LBSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author yangcheng
 * @version 1.0
 * @title: OrderController
 * @description: 订单Controller
 * @date 2019/6/10 0:35
 * @mail maiboyc@163.com
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(value = "订单管理", tags = {"订单相关操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderController extends BaseController {

	// 订单微服务Service
	@Autowired
	private OrderFeignService orderFeignService;

	@Autowired
	private UserService userService;


	/**
	 * 提交订单
	 *
	 * @param param 提交订单
	 * @return Response
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "提交订单", notes = "提交订单", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response submit(@RequestBody OrderSubmitParam param) {
		Response rs;
		try {
			rs = orderFeignService.submitOrder(param);
		} catch (Exception e) {
			log.error("提交订单异常" + "[" + e.getMessage() + "]");
			rs = Response.failed("提交订单异常" + e.getMessage());
		}
		return rs;
	}

	/**
	 * 支付订单
	 *
	 * @param param
	 * @return Response
	 */
	@PostMapping("/pay")
	@ApiOperation(value = "支付订单", notes = "支付订单", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response<PayResultDTO> pay(@RequestBody PayOrderParam param) {
		Response rs;
		try {
			//支付模式为xxx 支付方式为xxx的时候 才需要openid todo
			Response<ConsumerDto> rsp = userService.getById(param.getUserId());
			if (rsp != null && rsp.getData() != null) {
				ConsumerDto consumerDto = rsp.getData();
				Response<MemberDto> rspMember = userService.getMemberById(consumerDto.getMemberId());
				if (rspMember != null && rspMember.getData() != null) {
					MemberDto memberDto = rspMember.getData();
					param.setOpenId(memberDto.getWxOpenId());
				}
			}
			rs = orderFeignService.payOrder(param);
		} catch (Exception e) {
			log.error("支付订单异常" + "[" + e.getMessage() + "]");
			rs = Response.failed("支付订单异常" + e.getMessage());
		}
		return rs;
	}


	/**
	 * 取消订单
	 *
	 * @param orderId
	 * @return
	 */
	@DeleteMapping("/cancel/{orderId}")
	@ApiOperation(value = "取消订单", notes = "取消订单", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response cancel(@PathVariable("orderId") Long orderId) {
		Response rs;
		try {
			rs = orderFeignService.cancelOrder(orderId);
		} catch (Exception e) {
			log.error("取消订单异常" + "[" + e.getMessage() + "]");
			rs = Response.failed("取消订单异常" + e.getMessage());
		}
		return rs;
	}


	/**
	 * 退款订单
	 *
	 * @param orderId
	 * @return
	 */
	@DeleteMapping("/refund/{orderId}")
	@ApiOperation(value = "退款订单", notes = "退款订单", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response refund(@PathVariable("orderId") Long orderId) {
		Response rs;
		try {
			rs = orderFeignService.cancelOrder(orderId);
		} catch (Exception e) {
			log.error("退款订单异常" + "[" + e.getMessage() + "]");
			rs = Response.failed("退款订单异常" + e.getMessage());
		}
		return rs;
	}

	/**
	 * 删除订单
	 *
	 * @param orderId
	 * @return
	 */
	@DeleteMapping("/delete/{orderId}")
	@ApiOperation(value = "删除订单", notes = "删除订单", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response delete(@PathVariable("orderId") Long orderId) {
		Response rs;
		try {
			rs = orderFeignService.deleteOrder(orderId);
		} catch (Exception e) {
			log.error("删除订单异常" + "[" + e.getMessage() + "]");
			rs = Response.failed("删除订单异常" + e.getMessage());
		}
		return rs;
	}

	/**
	 * 订单列表
	 *
	 * @param param
	 * @return
	 */
	@PostMapping("/list")
	@ApiOperation(value = "订单列表", notes = "订单列表", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response<Page<OrderDTO>> list(@RequestBody OrderQueryParam param) {
		try {
			return orderFeignService.listOrder(param);
		} catch (Exception e) {
			log.error("查询订单列表异常" + "[" + e.getMessage() + "]");
			return Response.failed("查询订单列表异常" + e.getMessage());
		}
	}

	/**
	 * 订单详情
	 *
	 * @param params{orderId-订单ID,longitude-当前定位经度,latitude-当前定位维度}
	 * @return
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "订单详情", notes = "订单详情", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response<OrderDTO> detail(@RequestBody Map<String, Object> params) {
		try {
			Response<OrderDTO> rs = orderFeignService.viewOrder(Long.parseLong((String) params.get("orderId")));
			if (null != rs && rs.getCode() == 200 && null != rs.getData()) {
				OrderDTO order = rs.getData();
				if (order.getAddressId() == null) {
					order.setDistance(LBSUtil.getDistanceByLongitudeAndLatitude(order.getStoreLongitude().doubleValue(), order.getStoreLatitude().doubleValue(), Double.valueOf((String) params.get("longitude")), Double.valueOf((String) params.get("latitude"))));
				}
			}
			return rs;
		} catch (Exception e) {
			log.error("查询订单详情异常" + "[" + e.getMessage() + "]");
			return Response.failed("查询订单详情异常" + e.getMessage());
		}
	}


	/**
	 * 店铺订单列表
	 *
	 * @param param
	 * @return
	 */
	@PostMapping("/store/list")
	@ApiOperation(value = "店铺订单列表", notes = "店铺订单列表", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response<Page<OrderDTO>> listStoreOrder(@RequestBody StoreOrderQueryParam param) {
		try {

			if (param == null || param.getStoreId() == null) {
				return Response.failed("非法请求");
			}

			Response<Page<OrderDTO>> rs = orderFeignService.listStoreOrder(param);
			if (null != rs && rs.getCode() == 200 && null != rs.getData()) {
				rs.getData().getRecords().forEach(item -> {
					if (item.getAddressId() == null) {
						item.setDistance(LBSUtil.getDistanceByLongitudeAndLatitude(item.getStoreLongitude().doubleValue(), item.getStoreLatitude().doubleValue(), param.getLongitude(), param.getLatitude()));
					}
				});
			}
			return rs;
		} catch (Exception e) {
			log.error("查询订单列表异常" + "[" + e.getMessage() + "]");
			return Response.failed("查询订单列表异常" + e.getMessage());
		}
	}


	/**
	 * 更新订单状态订单
	 *
	 * @param param
	 * @return
	 */
	@PostMapping("/update/status")
	@ApiOperation(value = "更新订单状态", notes = "更新订单状态", consumes = MediaType.APPLICATION_JSON_VALUE)
	public Response<OrderDTO> updateOrderStatus(@RequestBody OrderMainStatusUpdateParam param) {
		try {

			if (param == null) {
				return Response.failed("非法请求");
			}

			return orderFeignService.updateOrderStatus(param);
		} catch (Exception e) {
			log.error("更新订单状态异常" + "[" + e.getMessage() + "]");
			return Response.failed("更新订单状态失败" + e.getMessage());
		}
	}

	// @PostMapping("/multiConditionQueryOrderPage")
	// @ApiOperation(value = "多条件查询订单", notes = "多条件查询订单", consumes = MediaType.APPLICATION_JSON_VALUE)
	// public Response<OrderMainQueryParam> multiConditionQueryOrderPage(@RequestBody OrderMainQueryParam param) {
	//
	// 	try  {
	// 		if (param == null) {
	// 			return Response.failed("非法请求");
	// 		}
	// 		return orderFeignService.multiConditionQueryOrderPage(param);
	// 	} catch (Exception e) {
	// 		log.error("多条件查询订单异常" + "[{}]", e.getMessage());
	// 		return Response.failed("多条件查询订单失败" + e.getMessage());
	// 	}
	// }
}
